package com.sec.android.app.myfiles.module.local.home;

import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.facade.cmd.DeleteRecordCmd;
import com.sec.android.app.myfiles.fragment.home.HomeFragment;
import com.sec.android.app.myfiles.info.AppConstants.AppUpdateBadge;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.AbsMenuImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.update.APKDownloadManager;
import com.sec.android.app.myfiles.update.UpdateChecker;
import com.sec.android.app.myfiles.update.UpdateChecker.StubUpdateCheckListener;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.util.VZCloudUtils;

public class HomeMenuImp extends AbsMenuImp implements StubUpdateCheckListener {

    protected boolean mNeedUpdate = false;

    public HomeMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        if (!AppFeatures.isChinaModel()) {
            checkUpdateStatus();
        }
        setBadgeText(menu);
        setMenuItemVisibility(menu, R.id.menu_edit, false);
        setMenuItemVisibility(menu, R.id.menu_import, false);
        setMenuItemVisibility(menu, R.id.menu_share, false);
        setMenuItemVisibility(menu, R.id.menu_add_shortcut, false);
        setMenuItemVisibility(menu, R.id.menu_show_hidden_files, false);
        setMenuItemVisibility(menu, R.id.menu_hide_hidden_files, false);
        setMenuItemVisibility(menu, R.id.menu_sync, false);
        setMenuItemVisibility(menu, R.id.menu_settings, true);
        setMenuItemVisibility(menu, R.id.menu_trash, false);
        setMenuItemVisibility(menu, R.id.menu_open, false);
        setMenuItemVisibility(menu, R.id.menu_open_in_a_new_window, false);
        setMenuItemVisibility(menu, R.id.menu_view_as, false);
        setMenuItemVisibility(menu, R.id.menu_empty, false);

        setMenuItemVisibility(menu, R.id.menu_search, true);
        setMenuItemVisibility(menu, R.id.menu_toggle_list_type, false);
        setMenuItemVisibility(menu, R.id.menu_sort_by, false);
        setMenuItemVisibility(menu, R.id.menu_create_folder, false);
        setMenuItemVisibility(menu, R.id.menu_clear_history, false);
        setMenuItemVisibility(menu, R.id.menu_clear_recent_history, mFragment.getRecentItemCount() > 0 && PreferenceUtils.getShowRecentFiles(mContext));
        setMenuItemVisibility(menu, R.id.menu_contact_us, UiUtils.checkSamsungMembersApp(mContext));
        setMenuItemVisibility(menu, R.id.menu_optimize_storage, UiUtils.supportOptimizeStorage(mContext));
        VZCloudUtils.prepareOptionsMenu(mFragment.getActivity(), menu, R.id.menu_vzcloud, canShowOptionsMenu());

        if (mFragment instanceof HomeFragment) {
            setMenuItemVisibility(menu, R.id.menu_manage_shortcut, ((HomeFragment) mFragment).getShortcutListItemCount() > 0 && NavigationManager.isPossibleToActionMode());
        }
    }

    @Override
    public boolean _onOptionsItemSelected(MenuItem item) {
        boolean bRet = true;
        int processId = mFragment.getProcessId();
        FileRecord curRecord = mFragment.getNavigationInfo().getCurRecord();
        switch (item.getItemId()) {
            case R.id.menu_remove:
                MyFilesFacade.deleteRecord(processId, mContext, mFragment, mFragment.getSelectedFile(),
                        DeleteRecordCmd.OperationProgress.DO_OPERATE);
                break;
            case R.id.menu_clear_recent_history:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.CLEAR_RECENT_FILE_HISTORY, null);
                MyFilesFacade.clearRecent(processId, mContext);
                break;
            case R.id.menu_contact_us:
                SamsungAnalyticsLog.sendLog(processId, SamsungAnalyticsLog.Event.CONTACT_US, null);
                MyFilesFacade.contactUs(processId, mContext);
                break;
            default:
                bRet = false;
                break;
        }

        if (!bRet) {
            bRet = super._onOptionsItemSelected(item);
        }
        return bRet;
    }

    @Override
    protected boolean homeAsUpEnabled() {
        return false;
    }

    @Override
    public String getActionBarTitle() {
        return mContext.getResources().getString(R.string.app_name);
    }

    private void checkUpdateStatus() {
        boolean isCheckMyFilesOnly = !UiUtils.isCloudGatewayEnabled(mContext);
        Context appContext = mContext.getApplicationContext();

        APKDownloadManager.Result result = APKDownloadManager.getInstance(appContext).needVersionUpdateAboutMyFiles(this, isCheckMyFilesOnly);
        if (!UpdateChecker.getInstance(appContext).isLoading()) {
            mNeedUpdate = result.needMyFilesUpdate || result.needCloudUpdate;
        }
    }

    @Override
    public void onUpdateCheckResult(boolean hasMyFilesUpdate, boolean hasCloudUpdate) {
        mNeedUpdate = hasMyFilesUpdate || hasCloudUpdate;

        PreferenceUtils.setBadgeCount(mContext, mNeedUpdate ? AppUpdateBadge.EXIST_NEW_APP_UPDATE : AppUpdateBadge.NO_APP_UPDATE);

        if (mNeedUpdate) {
            mActivity.invalidateOptionsMenu();
        } else {
            APKDownloadManager.getInstance(mContext.getApplicationContext()).setVersionChecking(false);
        }
    }
}
